import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Backend as B
import Gui.Global as G
import Gui.Views
import Gui.Models
import Gui.Components

ApplicationWindow {
    id: win
    width: 1114
    height: 718
    minimumHeight: 600
    minimumWidth: 800
    visible: true
    x: x = (Screen.width - width) / 2
    y: y = (Screen.height - height) / 2
    title: qsTr("我的音乐")
    flags: Qt.FramelessWindowHint | Qt.Window

    palette.highlight: "#71B087"

    LyricsDetail {
        id: lyricsDetail
        width: parent.width
        height: parent.height - playerBar.height
        clip: true
        albumCoverSource: playerBar.playBarViewModel.albumCover
        lyricsModel: playerBar.playBarViewModel.lyrics
        currentHightligt: playerBar.playBarViewModel.currentLyricIndex
        enter: Transition {
            NumberAnimation {
                property: "x"
                from: win.width
                to: 0
                easing.type: Easing.OutQuad
                duration: 300
            }
        }
        exit: Transition {
            NumberAnimation {
                property: "y"
                from: 0
                to: win.height
                easing.type: Easing.OutQuad
                duration: 300
            }
        }
        onVisibleChanged: {
            if (visible) {
                siderBar.x = -60;
                lyricsDetail.y = 0;
            }
        }

        onAboutToHide: {
            siderBar.x = 0;
        }
        onManuSetCurrentHighlight: index => {
            playerBar.playBarViewModel.setPositionByLyricIndex(index);
        }
    }

    SiderBar {
        id: siderBar
        WindowDragHandler {
            dragWindow: win
        }
        siderBtnsModel: NavModel {
            id: siderBarModel
        }
        clip: true
        color: palette.base
        height: parent.height
        width: 60

        Behavior on x {
            NumberAnimation {
                duration: 300
            }
        }

        onSelectedViewChanged: {
            const tmp = pagesRepeater.itemAt(selectedView);
            if (!tmp.item) {
                tmp.source = Qt.resolvedUrl(siderBarModel.get(selectedView).pageUrl);
            }
            mainContent.currentIndex = selectedView;
        }
    }

    TitleBar {
        id: titleBar
        dragWindow: win
        anchors.left: siderBar.right
        anchors.right: parent.right
        height: 50
    }
    PlayerBar {
        id: playerBar
        WindowDragHandler {
            dragWindow: win
        }
        property alias playBarViewModel: _playBarViewModel
        B.PlayerViewModel {
            id: _playBarViewModel
        }
        color: Qt.alpha(palette.base, 0.5)
        anchors.left: siderBar.right
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        height: 75
    }

    Playlist {
        id: playlist
        width: 300
        height: parent.height
        y: 0
    }

    StackLayout {
        id: mainContent
        anchors.left: siderBar.right
        anchors.right: parent.right
        anchors.top: titleBar.bottom
        anchors.bottom: playerBar.top
        currentIndex: -1

        Repeater {
            id: pagesRepeater
            model: siderBarModel.count

            Loader {
                Layout.fillHeight: true
                Layout.fillWidth: true
            }
        }
    }
    Connections {
        target: G.SignalBus

        function onOpenPlaylist() {
            playlist.open();
        }
        function onOpenLyricsDetail() {
            lyricsDetail.open();
            console.log("ff");
        }
    }
}
